package com.hk.Mgr.web.controller.app;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.PointsHistoryRes;
import com.hk.Mgr.web.message.kyc.PointsReq;
import com.hk.Mgr.web.message.kyc.PointsRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.Constant;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.entity.Points;
import com.hk.commons.entity.PointsHistory;
import com.hk.commons.entity.sys.SysUser;
import com.hk.service.IPointsHistory;
import com.hk.service.kyc.IPoints;
import com.hk.service.sys.ISysUser;

/**
 * @author lemon 获取积分流水信息
 */
@Controller
@RequestMapping(value = BaseCtrl.App + "/pointsHistoryCtrl")
public class AppPointsHistoryCtrl extends BaseCtrl {

	@Autowired
	private ISysUser userService;

	@Autowired
	private IPoints pointsService;

	@Autowired
	private IPointsHistory pointsHistoryService;
	
	/**
	 * 获取积分规则列表
	 * @param  typeName, curPage, pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public ResponseBase<List<PointsHistoryRes>> list(@RequestBody PointsReq req) {
		ResponseBase<List<PointsHistoryRes>> resp = new ResponseBase<List<PointsHistoryRes>>();
		List<PointsHistoryRes> resList = new ArrayList<PointsHistoryRes>();
		
		List<PointsHistory> hisList = new ArrayList<PointsHistory>();
		
		
		logger.info("app/pointsHistoryCtrl/list userId = " +  req.getUserId());
		 
		HqlFilter hqlFilter = new HqlFilter();
		 
		hqlFilter.addEQFilter("userId", req.getUserId()); 
		
		try {
			 hisList  = pointsHistoryService.findByFilter(hqlFilter);
			for(PointsHistory pointsHistory:hisList){
				PointsHistoryRes pointsHistoryRes = new PointsHistoryRes();
				ReqToModelHelper.copyReqValueToModel(pointsHistory, pointsHistoryRes);
			    //添加姓名
				pointsHistoryRes.setUserName(userService.getNameById(pointsHistory.getUserId()));
				
				Points points = pointsService.findById(pointsHistory.getPointsId());
				if(points != null){
					pointsHistoryRes.setPointsRuleDesc(points.getActDesc() );
				}
				
				resList.add(pointsHistoryRes);
			}
			resp.setResult(resList);
			resp.setSize(hisList.size());
			resp.setResultCode(StatusCode.SUCCESS.key());
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}


}
